declare let cacheGet: any;
module MC {
	class USER {
		public constructor() {
			$(() => {
				this.upUserInfo();
			});
		}

		public getInfo() {
			console.log('暂时无法使用');
		}

		/** 
	 * 获取用户类型
	 */
		public getType(): USERTYPE {
			return MC.USERTYPE.Type_gongsi_gongchang;

		}

		/***
		 * 本地缓存的用户信息
		*/
		get info(): any {
			let user = MC.Cache.getObj('user');
			if (!user) {
				return window.open('/h5/login/login.html', '_self');
			} else {
				return user;
			}
		}

		/** 
		 * 判断是否登陆
		 */
		public isLogin() {
			return MC.Cache.getObj('user');
		}
		/**
		 * 绑定加载完成的事件
		 * */	
		public Load(fun:Function){
			if(this.isLoad){
				fun && fun();
				return;
			}
			 window.addEventListener("mc_getInfo", (event:any) => {
				fun && fun();
			});			
		}


		private isLoad=false;

		/*** 获取最新的登录状态并更新用户信息 */
		public upUserInfo(fun: Function = null) {
			MC.api('getNowUserInfo', {},  (json: any)=>{
				this.isLoad=true;
				if (json == null) {
					fun && fun(null);
					MC.Cache.setObj('user', null);
					return;
				}
				var obj: any = {};
				obj.is_company = json.is_company;
				obj.is_factory = json.is_factory;
				obj.is_yingpin = json.is_yingpin;
				obj.head = json.head;
				obj.name = json.name;
				obj.id = json.id;
				obj.phone = json.phone;
				MC.Cache.setObj('user', obj);
				fun && fun(obj);
				var myEvent = new Event("mc_getInfo" );
				if (window.dispatchEvent) {
					window.dispatchEvent(myEvent);
				} else {
					(<any>window).fireEvent(myEvent);
				}
			});
		}

	}
	export let user = new USER();
	export enum USERTYPE {
		Type_yuangong = 1,////1个人用户
		Type_gongsi = 2,///2公司；
		Type_gongchang = 3,///3工厂
		Type_gongsi_gongchang = 4////4企业
	}
}